﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using app.Models;
using AmuTools;

namespace app.Api
{
    /*
        数据库的初始化，备份等操作

        初始化的目的是保证一些必要的数据添加到数据库中，甚至可以自动建立表（通过执行sql文件代码）等
    */
    public class ApiDatabase : ApiBase
    {
        public ApiDatabase(Controller ctx) : base(ctx) { }

        #region 自定义请求参数
        // 作者
        string req_author { get { return GetStringParam(new ReqStringParamOption { Name = "author", Level = REQPARAMLEVEL.FREE }); } }

        #endregion

        public void Do(string action)
        {
            switch (action)
            {
                case "init":
                    if(req_password != System.Configuration.ConfigurationManager.AppSettings["CreateDatabasePassword"])
                    {
                        ThrowStatus(STATUS.NO_PERMISSION, "初始化数据库，认证码错误！");
                    }
                    G.DB.CreateDataBase(new List<Type> {
                        typeof(AdminRole), typeof(Admin), typeof(MemberRole), typeof(Member), typeof(Article),
                        typeof(Finance), typeof(Investment), typeof(UploadedFile), typeof(Abutment)
                    }, G.DB.GetSqlFiles(ctx.Server.MapPath("~/") + "Models/StoredProcedures/"));
                    ResponseJson(new Result());
                    break;
                case "backups":
                    break;
                default: break;
            }
        }
    }


}